package com.battleship;

public class GuesserTaylor2 implements Guesser {

    private Game game;
    private boolean gameOver = false;

    @Override
    public void play(Game game) {
        setGame(game);

        // left
        guess(1, 1);
        guess(1, 3);
        guess(1, 5);
        guess(1, 7);
        guess(1, 9);

        // down
        guess(10, 2);
        guess(10, 4);
        guess(10, 6);
        guess(10, 8);
        guess(10, 10);

        // right
        guess(8, 10);
        guess(6, 10);
        guess(4, 10);
        guess(2, 10);

        // up
        guess(1, 9);
        guess(1, 7);
        guess(1, 5);
        guess(1, 3);

        // right
        guess(2, 2);
        guess(4, 2);
        guess(6, 2);
        guess(8, 2);

        // down
        guess(9, 3);
        guess(9, 5);
        guess(9, 7);
        guess(9, 9);

        // left
        guess(7, 9);
        guess(5, 9);
        guess(3, 9);

        // up
        guess(2, 8);
        guess(2, 6);
        guess(2, 4);

        // right
        guess(3, 3);
        guess(5, 3);
        guess(7, 3);

        // down
        guess(8, 4);
        guess(8, 6);
        guess(8, 8);

        // left
        guess(6, 8);
        guess(4, 8);

        // up
        guess(3, 7);
        guess(3, 5);

        // right
        guess(4, 4);
        guess(4, 6);

        // down
        guess(7, 5);
        guess(7, 7);

        // left
        guess(5, 7);

        guess(4, 6);

        guess(5, 5);
    }

    public void setGame(Game game) {
        this.game = game;
    }

    protected int guess(int x, int y) {
        if (gameOver) {
            return 10;
        }
        int guess = game.guess(new Point(x, y));
        switch (guess) {
            case 1:
                if (x < 10) {
                    guess(x + 1, y);
                }
                if (x > 1) {
                    guess(x - 1, y);
                }
                if (y > 1) {
                    guess(x, y - 1);
                }
                if (y < 10) {
                    guess(x, y + 1);
                }
                break;
            case 2:
                if (game.getShipsLeft() == 0) {
                    gameOver = true;
                }
        }
        return guess;
    }
}
